package jquery.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import member.domain.Member;

import org.json.JSONObject;

/**
 * Servlet implementation class jQueryServlet
 */
public class jQueryServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		
		String cmd = request.getParameter("cmd");
		if(cmd.equals("getMemberByIdJSON")){
			getMemberByIdJSON(request, response);
		}else if(cmd.equals("getData")){
			getData(request, response);
		}else if(cmd.equals("getMemberById")){
			getMemberById(request, response);
		}else if(cmd.equals("run")){
			run(request,response);
		}
	}
	public void run(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	//요청 파라미터 조회
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		String number = request.getParameter("number");
		String isSend = request.getParameter("isSend")==null ? "전송안함":"전송";
		String reqParam	= id+","+name+","+number+","+isSend;
		System.out.println(reqParam);
		PrintWriter out = response.getWriter();
		out.println(reqParam);
	}
	
	public void getMemberById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		PrintWriter out = response.getWriter();
		//조회회원 정보 : id, name:이순신, 나이:30, 주소:인천
		out.println("ID : "+id+"<br>");
		out.println("이름 : 이순신 <br>");
		out.println("나이 : 30 <br>");
	}
	
	//ID : abc<br> \n 이름 :이순신<br> \n나이 : 30<br>
		public void getData(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String reqParam = request.getParameter("param");
		PrintWriter out = response.getWriter();
//		if(true){
//			throw new ServletException("오류발생");
//		}
		out.println("요청파라미터 : "+reqParam+"<br>");
		out.println("요청 방식 : "+request.getMethod());
	}
	public void getMemberByIdJSON(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//1. 요청파라미터 조회
		String id = request.getParameter("id");
		//2. 비지니스 로직
		Member m = new Member(id, "1111", "이철수", "901121", "1111221", 20000);
		//3. 응답 Member -> JS 객체 형태의 문자열(JSON)
		JSONObject jo = new JSONObject(m);
		PrintWriter out = response.getWriter();
		out.print(jo);
		
		
	}
}












